package com.mock.action.services.programme;

import java.util.ArrayList;
import java.util.List;

import com.mock.action.AbstractAction;
import com.mock.dao.ContactDao;
import com.mock.dao.ProgrammeDao;
import com.mock.dao.impl.ContactDaoImpl;
import com.mock.dao.impl.ProgrammeDaoImpl;
import com.mock.model.Contact;
import com.mock.model.Programme;

public class ProgrammeAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 983859763659724906L;


	/**
	 * 
	 */
	
	/* Bussiness Method */
	@Override
	public String execute() throws Exception {
		programmes = programmeDao.getAllProgramme();
		return SUCCESS;
	}

	public String addProgramme() {
		Contact contact = null;
		if (contactId != null) {
			contact = contactDao.getContactById(contactId);
		}
		programmeDao.add(new Programme(name, description, active, contact));
		return SUCCESS;
	}

	public String viewProgramme() {
		bean = programmeDao.getById(Programme.class, programmeId);
		return SUCCESS;
	}

	public String updateProgramme() {
		Programme programme = programmeDao.getProgrammeById(id);
		Contact contact = null;
		if (contactId != null) {
			contact = contactDao.getContactById(contactId);
		} 
		programme.setName(name);
		programme.setDescription(description);
		programme.setContact(contact);
		programme.setActive(active);
		programmeDao.update(programme);
		return SUCCESS;
	}

	public String lookUpContact() {
		List<Contact> contacts = contactDao.getAllContact();
		for (Contact contact : contacts) {
			ArrayList<String> newArray = new ArrayList<String>();
			newArray.add(Long.toString(contact.getId()));
			newArray.add(contact.getFirstname() + " " + contact.getSurname());
			newArray.add(contact.getMobilePhone());
			newArray.add(contact.getEmail());
			newArray.add(contact.getContactType().getType());

			if (contact.isActive()) {
				newArray.add("Yes");
			} else {
				newArray.add("No");
			}
			contactJson.add(newArray);
		}
		return SUCCESS;
	}

	// get All data - json

	public String getAllProgramme() {
		for (Programme p : programmeDao.getAllProgramme()) {
			nameProgrammes.add(p.getName());
		}
		return SUCCESS;
	}

	public String setProgrammeActive() {
		System.out.println("id: " + programmeId);
		Programme bean = programmeDao.getById(Programme.class, programmeId);
		bean.setActive(true);
		programmeDao.update(bean);
		return SUCCESS;
	}

	public String changeActive() {
		ProgrammeDao dao = new ProgrammeDaoImpl();
		Programme bean = dao.getById(Programme.class, id);
		bean.setActive(true);
		dao.update(bean);
		return SUCCESS;
	}

	/* Getter & Setter */
	private Programme bean;
	private long programmeId;
	private List<Programme> programmes;
	private long id;
	private String name;
	private String description;

	private Long contactId;
	private boolean active;

	private ContactDao contactDao = new ContactDaoImpl();
	private ProgrammeDao programmeDao = new ProgrammeDaoImpl();
	//
	private Programme programme;

	private List<String> nameProgrammes = new ArrayList<String>();
	// address all

	private List<ArrayList<String>> contactJson = new ArrayList<ArrayList<String>>();
	// string postcode
	private String postcode;

	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getContactId() {
		return contactId;
	}

	public void setContactId(Long contactId) {
		this.contactId = contactId;
	}

	public boolean getActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public long getProgrammeId() {
		return programmeId;
	}

	public void setProgrammeId(long programmeId) {
		this.programmeId = programmeId;
	}

	public List<Programme> getProgrammes() {
		return programmes;
	}

	public void setProgrammes(List<Programme> programmes) {
		this.programmes = programmes;
	}

	public Programme getBean() {
		if (programmeId != -1L) {
			bean = new ProgrammeDaoImpl().getProgrammeById(programmeId);
		}
		return bean;
	}

	public void setBean(Programme bean) {
		this.bean = bean;
	}

	public List<ArrayList<String>> getContactJson() {
		return contactJson;
	}

	public void setContactJson(List<ArrayList<String>> contactJson) {
		this.contactJson = contactJson;
	}

	public List<String> getNameProgrammes() {
		return nameProgrammes;
	}

	public void setNameProgrammes(List<String> nameProgrammes) {
		this.nameProgrammes = nameProgrammes;
	}

	// setter and getter for programe...
	public Programme getProgramme() {
		return programme;
	}

	public void setProgramme(Programme programme) {
		this.programme = programme;
	}

}
